package zuoshen_video;

import java.util.Arrays;

import org.junit.Test;

import utils.ArrayGenerator;

public class MoveLeftAndRight {

    //小于等于n移动到n左边，大于n移动到n右边
    //老版partion，解决不了最后的那个元素的问题
    public void move1(int[] nums, int n) {
        int i = 0, j = nums.length - 1;
        while (i < j) {
            while (i < j && nums[j] > n) j--;
            while (i < j && nums[i] <= n) i++;
            swap(nums, i, j);
        }
    }

    //新版partion，小于等于区域扩张法
    public void move(int[] nums, int n) {
        int lo = -1, hi = 0;
        while (hi < nums.length) {
            if (nums[hi] <= n) { //当hi的位置值小于等于n，将其与lo交换，并将(0, lo]区间扩大一个
                swap(nums, lo + 1, hi);
                lo++;
            } //nums[hi] > num, 可以跳过
            hi++;
        }
    }

    //荷兰国旗问题：化为三个区域[小于 等于 大于]n
    //解法错误，未知bug
    public void holand1(int[] nums, int n) {
        int l = 0, r = 0, e = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > n) r++;
            else if (nums[i] < n) l++;
            else e++;
        }
        int idx = l;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == n) {
                swap(nums, i, idx++);
                if (idx == l + e) break;
            }
        }

        int lo = -1, hi = nums.length;
        while (lo < l && hi >= l + e) {
            while (lo < l - 1 && nums[lo + 1] < n) lo++;
            while (hi > l + e && nums[hi - 1] > n) hi--;
            swap(nums, lo + 1, hi - 1);
        }

    }

    //正确，但是多遍历了半个多数组
    public void holand11(int[] nums, int n) {
        int hi = nums.length, len = hi, lo = -1;
        int i = 0, j = len - 1;
        for (; i < len; i++) {
            if (nums[i] < n) {
                swap(nums, lo + 1, i);
                lo++;
            }
        }

        for (; j >= lo; j--) {
            if (nums[j] > n) {
                swap(nums, hi - 1, j);
                hi--;
            }
        }
    }

    //只遍历一遍数组
    public void holand(int[] nums, int n) {
        int len = nums.length,  lo = -1, hi = len, cur = 0; 
        while (cur < hi) {
            if (nums[cur] > n) {
                //大于区域扩张，cur不变，继续考察新换过来的数
                swap(nums, cur, hi - 1);
                hi--;
            } else if (nums[cur] == n) {
                //跳过
                cur++;
            } else {
                //小于区域扩张
                swap(nums, cur, lo + 1);
                lo++;
                cur++;
            }
        }
    }


    public void quickSort(int[] nums, int left, int right) {
        if (left < right) {
            int[] p = partion(nums, left, right);
            quickSort(nums, left, p[0] - 1);
            quickSort(nums, p[1] + 1, right);
        }
    }

    public int[] partion(int[] nums, int lo, int hi) {
        int i = lo - 1, j = hi + 1, cur = lo;
        int pivot = nums[(int)Math.random() * (hi - lo + 1) + lo];
        System.out.println(pivot);
        while (cur < j) {
            if (nums[cur] == pivot) {
                cur++;
            } else if (nums[cur] > pivot) {
                swap(nums, cur, --j);
            } else {
                swap(nums, ++i, cur++);
            }
        }
        return new int[]{i + 1, j - 1}; //返回pivot的范围
    }


    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    @Test
    public void test() {
        int[] nums = ArrayGenerator.getArray(10, 10);
        nums[3] = 5;
        nums[8] = 5;
        // nums = new int[]{7, 3, 5, 5, 7, 6, 6, 5, 5, 6};
        System.out.println(Arrays.toString(nums));
        // move(nums, 5);
        // holand(nums, 5);
        // quickSort(nums, 0, nums.length - 1);
        partion(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));

        System.out.println(-1 / 2);

    }
}
